package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.OverduePhaseStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhOverdueCollectionPhoneMapper;
import com.zjdiepu.www.dao.dh.DhOverdueListMapper;
import com.zjdiepu.www.dao.yw.YwBusinessExtrasMapper;
import com.zjdiepu.www.ibiz.dh.IDhOverdueCollectionPhoneService;
import com.zjdiepu.www.model.dh.dto.DhOverdueCollectionPhone;
import com.zjdiepu.www.model.dh.dto.DhOverdueList;
import com.zjdiepu.www.model.dh.ext.DhOverdueCollectionPhoneExt;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月14日 14时26分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhOverdueCollectionPhoneServiceImpl extends BaseService implements IDhOverdueCollectionPhoneService {
	public static final Logger logger = LoggerFactory.getLogger(DhOverdueCollectionPhoneServiceImpl.class);

	@Autowired
	private DhOverdueCollectionPhoneMapper dhOverdueCollectionPhoneMapper;
	
	@Autowired
	private DhOverdueListMapper dhOverdueListMapper;
	
	@Autowired
	private YwBusinessExtrasMapper ywBusinessExtrasMapper;
	
	@Override
	public void saveUpdateAndDeleteAll(
			DhOverdueCollectionPhone dhOverdueCollectionPhone, OperateType add, AbstractSysUser currentUser) {
		
		dhOverdueCollectionPhone.initInsertData(dhOverdueCollectionPhone.getModifier(), dhOverdueCollectionPhone.getModifierName());
		dhOverdueCollectionPhoneMapper.insert(dhOverdueCollectionPhone);
		
		//更新逾期信息表电催人id和逾期阶段
        DhOverdueList dhOverdueList= new DhOverdueList();
        dhOverdueList.setBusiId(dhOverdueCollectionPhone.getBusiId());
        DhOverdueList dhOverdueList1=dhOverdueListMapper.selectRefined(dhOverdueList);
        dhOverdueList1.setIdOperatorPhone(currentUser.getId());
        dhOverdueList1.setPhaseOverdue(OverduePhaseStatus.DIAN_CUI.getCode());
        if(StringUtils.isNotBlank(dhOverdueCollectionPhone.getStatusRepayment())&& dhOverdueCollectionPhone.getStatusRepayment().equals("01")){
        	dhOverdueList1.setOverdueStatus(30);
        }
        dhOverdueList1.initUpdateData(currentUser.getId(), currentUser.getName());
        dhOverdueListMapper.updateByPrimaryKeySelective(dhOverdueList1);
        
        //更新到业务信息-附加信息表电催人id
        YwBusinessExtras ywBusinessExtras1=new YwBusinessExtras();
        ywBusinessExtras1.setBusiId(dhOverdueCollectionPhone.getBusiId());
        ywBusinessExtras1.setCodeExtras("id_operator_phone");
        ywBusinessExtras1=ywBusinessExtrasMapper.selectRefined(ywBusinessExtras1);
        if(ywBusinessExtras1!=null){
        	ywBusinessExtras1.setValueExtras(currentUser.getId());
        	ywBusinessExtras1.initUpdateData(currentUser.getId(), currentUser.getName());
        	ywBusinessExtrasMapper.updateByPrimaryKeySelective(ywBusinessExtras1);
        }else{
        	YwBusinessExtras ywBusinessExtras2=new YwBusinessExtras();
        	ywBusinessExtras2.setBusiId(dhOverdueCollectionPhone.getBusiId());
        	ywBusinessExtras2.setCodeExtras("id_operator_phone");
        	ywBusinessExtras2.setTypeExtras("overdue");
        	ywBusinessExtras2.setValueExtras(currentUser.getId());
        	ywBusinessExtras2.setNameExtras("逾期催收");
        	ywBusinessExtras2.initInsertData(currentUser.getId(), currentUser.getName());
        	ywBusinessExtrasMapper.insert(ywBusinessExtras2);
        }
        
        YwBusinessExtras ywBusinessExtras3=new YwBusinessExtras();
        ywBusinessExtras3.setBusiId(dhOverdueCollectionPhone.getBusiId());
        ywBusinessExtras3.setCodeExtras("phase_overdue");
        ywBusinessExtras3=ywBusinessExtrasMapper.selectRefined(ywBusinessExtras3);
        if(ywBusinessExtras3!=null){
        	ywBusinessExtras3.initUpdateData(currentUser.getId(), currentUser.getName());
        	ywBusinessExtras3.setValueExtras(OverduePhaseStatus.DIAN_CUI.getCode());
        	ywBusinessExtrasMapper.updateByPrimaryKeySelective(ywBusinessExtras3);
        }else{
        	YwBusinessExtras ywBusinessExtras2=new YwBusinessExtras();
        	ywBusinessExtras2.setBusiId(dhOverdueCollectionPhone.getBusiId());
        	ywBusinessExtras2.setCodeExtras("phase_overdue");
        	ywBusinessExtras2.setTypeExtras("overdue");
        	ywBusinessExtras2.setValueExtras(OverduePhaseStatus.DIAN_CUI.getCode());
        	ywBusinessExtras2.setNameExtras("逾期催收");
        	ywBusinessExtras2.initInsertData(currentUser.getId(), currentUser.getName());
        	ywBusinessExtrasMapper.insert(ywBusinessExtras2);
        }
		
	}
	@Override
	public void saveUpdateAndDelete(DhOverdueCollectionPhone dhOverdueCollectionPhone, OperateType operType) {
		switch (operType) {
		case add:
			dhOverdueCollectionPhone.initInsertData(dhOverdueCollectionPhone.getModifier(), dhOverdueCollectionPhone.getModifierName());
			dhOverdueCollectionPhoneMapper.insert(dhOverdueCollectionPhone);
			break;
		case edit:
			dhOverdueCollectionPhone.initUpdateData(dhOverdueCollectionPhone.getModifier(), dhOverdueCollectionPhone.getModifierName());
			// 影响的记录数
			int updateCount = dhOverdueCollectionPhoneMapper.updateByPrimaryKeySelective(dhOverdueCollectionPhone);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhOverdueCollectionPhone.initDeleteData(dhOverdueCollectionPhone.getModifier(), dhOverdueCollectionPhone.getModifierName());
			// 影响的记录数
			int deleteCount = dhOverdueCollectionPhoneMapper.updateByPrimaryKeySelective(dhOverdueCollectionPhone);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhOverdueCollectionPhone> selectByExt(DhOverdueCollectionPhoneExt dhOverdueCollectionPhoneExt) {
		return dhOverdueCollectionPhoneMapper.selectByExt(dhOverdueCollectionPhoneExt);
	}
	
	@Override
	public int countByExt(DhOverdueCollectionPhoneExt dhOverdueCollectionPhoneExt){
		return dhOverdueCollectionPhoneMapper.countByExt(dhOverdueCollectionPhoneExt);
	}
	
	@Override
	public DhOverdueCollectionPhone selectById(String id) {
		return dhOverdueCollectionPhoneMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhOverdueCollectionPhoneMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhOverdueCollectionPhone dhOverdueCollectionPhone) {
		return dhOverdueCollectionPhoneMapper.countRefined(dhOverdueCollectionPhone);
	}

	@Override
	public DhOverdueCollectionPhone selectRefined(DhOverdueCollectionPhone dhOverdueCollectionPhone) {
		return dhOverdueCollectionPhoneMapper.selectRefined(dhOverdueCollectionPhone);
	}

	@Override
	public boolean repeat(DhOverdueCollectionPhone dhOverdueCollectionPhone) {
		int count = countRefined(dhOverdueCollectionPhone);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhOverdueCollectionPhone.getId())) {
			return true;
		}
		DhOverdueCollectionPhone dbItem = selectRefined(dhOverdueCollectionPhone);
		if(null == dbItem) {
			return false;
		}
		return !dhOverdueCollectionPhone.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhOverdueCollectionPhoneMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public List<DhOverdueCollectionPhone> selectCountByPhone(String value2) {
		return dhOverdueCollectionPhoneMapper.selectCountByPhone(value2);
	}

	
}
